<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" class="tl-dialog" width="1200px" @open="open" @close="close" :close-on-click-modal="false" :close-on-press-escape="false">
    <div class="app-container">
      <!-- 工具栏 -->
      <el-row>
        <tl-list-toolbar ref="toolbar" @refresh="doRefresh" @exportExcel="doExportExcel" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :isDisplayAdd="false" :isDisplayDeleteBatch="false" :isDisplayExport="isSys" :isDisplayRight="false"></tl-list-toolbar>
      </el-row>
      <el-table class="tl-table" :height="getHeight() * 0.5" style="width:100%" ref="mainTable" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" border fit>
        <el-table-column prop="dispprojectmanagercode" label="项目经理姓名" width="120px" header-align="center" align="center" :show-overflow-tooltip="true" v-if="sourceType === 'managerForm'"></el-table-column>
        <el-table-column prop="deptname" label="项目部门" width="120px" header-align="center" align="center" :show-overflow-tooltip="true" v-if="sourceType === 'deptForm'"></el-table-column>
        <el-table-column prop="projectstateinitwait" label="待立项" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstateinitstart" label="立项中" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstateinitend" label="立项不通过" width="100px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstateappointment" label="任命阶段" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstateplan" label="策划阶段" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstateexecute" label="执行阶段" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstatenormal" label="正常结项" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectstateabnormal" label="异常结项" width="80px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="abnormalstateevent" label="超期任务数" width="100px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="abnormalstatemilestone" label="超期里程碑及任务数" width="120px" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="participatemoney"  label="项目金额(万元)" width="120px" header-align="center" align="right" :show-overflow-tooltip="true"></el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script>
// import checkPermission from '@/utils/permission' // 权限判断函数
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import { getAction, putAction, postAction } from '@tapui/utils/http'
import { formatDate } from '@tapui/utils/date'
import { exportExcelAxio } from '@tapui/api/office'
import { filterObjectNull, rmbFormat } from '@tapui/utils/util'

export default {
  name: 'leadSummary',
  components: {},
  mixins: [mixin_list],
  data() {
    return {
      urlPrefix: '/mpm/projectpolymerization/',
      url: {
        summaryList: '/mpm/project/getSummaryList',
        exportExcel: '/mpm/project/exportBigExcel'
      },
      title: '',
      visible: false,
      // 排序参数
      sorterInfo: {
        sidx: '',
        sord: 'asc'
      }
    }
  },
  props: {
    sourceType: {
      type: String,
      default() {
        return ''
      }
    },
    queryParams: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  computed: {
    isSys() {
      if (this.queryParams.qry_purchasepattern) {
        return false
      } else {
        return true
      }
    }
  },
  methods: {
    open() {
      this.$nextTick(() => {
        this.loadData()
      })
    },
    close() {
      this.visible = false
    },
    beforeExportData() {
      return true
    },
    afterExportData() {},
    /** 重写导出Excel*/
    doExportExcel() {
      if (!this.beforeExportData()) return
      let cols = []
      this.$refs.mainTable.$children.forEach((col) => {
        if (col.$options._componentTag === 'el-table-column') {
          if (col.$options.propsData.prop) {
            cols.push(col.$options.propsData.prop + ';' + col.$options.propsData.label)
          }
        }
      })
      let url = this.url.exportExcel
      var params = { cols: cols.join(','), sourceType: this.sourceType }
      var fileParam = {}
      if (this.sourceType === 'managerForm') {
        Object.assign(fileParam, { fileName: '统计数据(按项目经理)' })
      } else if (this.sourceType === 'deptForm') {
        Object.assign(fileParam, { fileName: '统计数据(按部门)' })
      } else {
      }
      Object.assign(params, this.sorterInfo, this.queryParams, fileParam)
      params = filterObjectNull(params)
      exportExcelAxio({ url: url, data: params })
        .then((res) => {
          var reader = new FileReader()
          reader.readAsText(res.data, 'utf-8')
          reader.onload = () => {
            try {
              var info = JSON.parse(reader.result)
              this.$tap.alert(info.message)
            } catch (e) {
              var isTaskExport = res.headers.istaskexport
              if (isTaskExport === '1') {
                this.$tap.alert('系统正在导出，请到excel导出任务页面下载')
              } else {
                var blob = new Blob([res.data], { type: 'application/octet-stream;charset=utf-8' })
                var filename = decodeURI(res.headers['filename'])
                if (typeof filename === 'undefined') {
                  filename = '导出数据.xlsx'
                }
                var tempObjectUrl = window.URL.createObjectURL(blob)
                var aLink = document.createElement('a')
                aLink.style.display = 'none'
                aLink.download = filename
                aLink.href = tempObjectUrl
                document.body.appendChild(aLink)
                aLink.click()
                document.body.removeChild(aLink)
                window.URL.revokeObjectURL(tempObjectUrl)
                this.afterExportData()
              }
            }
          }
        })
        .catch((error) => {
          this.$tap.alert(error, 'warning')
        })
    },
    beforeLoadData() {
      if (this.sourceType === 'managerForm') {
        this.title = '统计数据(按项目经理)'
        this.sorterInfo.sidx = 'projectmanagercode'
      } else if (this.sourceType === 'deptForm') {
        this.title = '统计数据(按部门)'
        this.sorterInfo.sidx = 'deptname'
      }
      return true
    },
    loadData(paramMap) {
      if (!this.url.summaryList) {
        this.$tap.alert('请设置list地址!', 'warning')
        return
      }
      if (!this.beforeLoadData()) return
      var params = { sourceType: this.sourceType }
      // 判断props是否将值传入
      if (this.sourceType === '') {
        return
      }
      Object.assign(params, this.sorterInfo, this.queryParams)
      // 数据查询
      this.loading = true
      getAction(this.url.summaryList, params)
        .then((res) => {
          if (res.success) {
            this.datas = res.result
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterLoadData()
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    getHeight() {
      var div
      var h
      div = document.createElement('div')
      div.style.height = '100vh'
      div.style.maxHeight = 'none'
      div.style.boxSizing = 'content-box'
      document.body.appendChild(div)
      h = div.clientHeight
      document.body.removeChild(div)
      return h
    }
  }
  // mounted() {}
  // watch: {
  //   queryParams(newValue, oldValue) {
  //     debugger
  //     this.newQueryParams = newValue
  //   }
  // }
}
</script>
<style scoped>
.tl-dialog >>> .el-dialog__body {
  padding: 5px 5px !important;
}
</style>
